<?php
  
  /*
   * Validaiton for Document content
   */

  function  rules_form_validation($form) {

    $stat = new set_static_variable();

//check the corresponding concerned dept name
    //chk_concerned_dept($form['field_rules_dep']);
    // Duplication in title
    //check_title_duplication($form['title'], $form['type']['#value'], $form['field_rules_dep'], 'field_rules_dep', $stat->get_nid());
    $stat->set_nid(0);
	
	// maximum length validation for Title and Description Field
    maxlength_validation($form['title']['#value'], 255, $form['title']['#title'], 'title');

    // Bad character validations 
    chkbadchar($form['title']['#value'], $form['title']['#title'], 'title');
    chkbadchar($form['rules_field_keyword']['und'][0]['value']['#value'], 
                                            $form['rules_field_keyword']['und'][0]['value']['#title'], 'rules_field_keyword');  
	chkbadchar($form['rules_field_des']['und'][0]['value']['#value'], 
                                            $form['rules_field_des']['und'][0]['value']['#title'], 'rules_field_des');
   	//chkbadchar($form['field_rules_url'][0]['value']['#value'],$form['field_rules_url']['#title'],'field_rules_url');
	chkbadchar($form['rules_field_short']['und'][0]['value']['#value'], $form['rules_field_short']['und'][0]['value']['#title'], 'rules_field_short');
	chkbadchar($form['rules_field_titleact']['und'][0]['value']['#value'], $form['rules_field_titleact']['und'][0]['value']['#title'], 'rules_field_titleact');
	
	// minimum length charcter
  	minilength_validation($form['title']['#value'], 5, $form['title']['#title'], 'title');
  	minilength_validation($form['rules_field_keyword']['und'][0]['value']['#value'], 5, $form['rules_field_keyword']['und'][0]['value']['#title'],'rules_field_keyword');
  	minilength_validation($form['rules_field_des']['und'][0]['value']['#value'], 5, $form['rules_field_des']['und'][0]['value']['#title'], 'rules_field_des');
	
	//Date Validation
	check_scheduler_date_expire($form['scheduler_settings']['unpublish_on']['date']['#value'],'unpublish_on');
	check_date_expire($form['field_rules_date_released']['und'][0]['value']['date']['#value'],$form['field_rules_date_released']['und'][0]['value']['date']['#title']);
	
	// Rules Number 
	check_integer($form['rules_field_actnumber']['und'][0]['value']['#value'],$form['rules_field_actnumber']['und'][0]['value']['#title'],'rules_field_actnumber');
	
	// Rules Year 
	check_integer($form['rules_field_rulesyear']['und'][0]['value']['#value'],$form['rules_field_rulesyear']['und'][0]['value']['#title'],'rules_field_rulesyear');
	years($form['rules_field_rulesyear']['und'][0]['value']['#value'],$form['rules_field_rulesyear']['und'][0]['value']['#title'],'rules_field_rulesyear');
	
	// Commencement date
	
	check_date_expire($form['rules_field_date']['und'][0]['value']['date']['#value'],$form['rules_field_date']['und'][0]['value']['date']['#title']);
	

    // Form type validation
   // $form_type = $form['field_rules_avail'];
//	
//	if ($form_type['value']['#value'] == 'Online-Available') {
//		
//	  $download = $form_type['value']['#value'] == 'Downloadable';
//	  $down =  $form['field_rules_fileup'][0]['#value']['filename']; 
//	  if(!empty($down) ) {
//		
//	  form_set_error('field_rules_fileup', t('Please remove Download pdf', array(
//														   '%name' => $form['field_rules_fileup']['#title'])));
//	  }
//      $reference_url = $form['field_rules_url'][0]['value']['#value'];
//      $reference_url = url_validation($reference_url);
//	  	  
//	  if(isURL($reference_url) == 0 && $reference_url != '') {
//      form_set_error('field_rules_url', t('Not a valid URL given in %name field', array(
//                                                   '%name' => $form['field_rules_url']['#title'])));    
//    }
//    elseif ($reference_url == '') {
//      form_set_error('field_rules_url', t(' %name field is required', array(
//                                                   '%name' => $form['field_rules_url']['#title'])));
//    }      
//  }
//  elseif ($form_type['value']['#value'] == 'Downloadable') {
//  	$online = $form_type['value']['#value'] == 'Online-Available';
//	$online = '';
//    // Validating the file size   
//    $field_obj = $form['field_rules_fileup']; 
//	$file_allowed_obj = $form['field_form_allowedfiletype'];
//    file_upload_validation($field_obj, $file_allowed_obj, 'field_rules_fileup');  
//  } 
//   

   // function documents_form_validation()
   url_validation($form['field_rules_url']['und'][0]['value']['#value']);
  // $form_type = $form['field_rules_avail'];
	
    //if ($form_type['value']['#value'] == 'OnlineAvailable') {
		
	//$reference_url = $form['field_rules_url'][0]['value']['#value'];
	//if(isURL($reference_url) == 0 && $reference_url != '') {
      //form_set_error('field_rules_url', t('Not a valid URL given in %name field', array('%name' => $form['field_rules_url']['und'][0]['value']['#value']['#title'])));    
   // }
    //elseif ($reference_url == '') {
      //form_set_error('field_rules_url', t(' %name field is required', array('%name' => $form['field_rules_url']['und'][0]['value']['#value']['#title'])));
    //}
  //}
  
  if ($form['field_rules_avail']['und']['#value'] == 'Downloadable') { 
    // Validating the file size   
    //$field_obj = $form['field_act_file_upload']; 
	//$file_allowed_obj = $form_type;
	//print_r($form['field_act_file_upload']);
	$filename = $form['field_rules_fileup']['und'][0]['#file']; 
	$filename->filename;
    file_upload_validation_D7($filename->filename,$form['field_rules_avail']['und']['#title']);  
  
  }
   

}
  

  function rules_presave_validation($node) {
    $reference_url = $node->field_rules_url['und'][0]['value'];
    $node->field_rules_url['und'][0]['value'] = url_validation($reference_url);
  } // function form_presave_validation()

function rules_load_validation() {
  $stat = new set_static_variable();
  $stat->set_edit('yes');
}
